package com.flyme.module.base.provider.service.impl;

import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysUsersturecord;
import com.flyme.module.base.provider.mapper.SysUsersturecordMapper;
import com.flyme.module.base.provider.service.SysUsersturecordService;
import org.springframework.stereotype.Service;

/**
 * 员工学习记录
 */
@Service
public class SysUsersturecordServiceImpl extends BaseServiceImpl<SysUsersturecordMapper, SysUsersturecord> implements SysUsersturecordService {

    /**
     * 查询用户是否有该视频记录
     */
    public Integer countPlan(Long userId, Long planId, Long catalogId){
        return count(q().eq("userId", userId).eq("complanId", planId).eq("catalogId", catalogId));
    }

    /**
     * 查询用户该计划一共观看的视频数量
     */
    public Integer countByPlan(Long userId, Long planId){
        return count(q().eq("userId", userId).eq("complanId", planId));
    }

    /**
     * 根据计划观看过的视频的数量
     */
    public Integer countByPlanNum(Long complanId){
        return count(q().eq("complanId", complanId));
    }

    /**
     * 根据学习计划及部门和企业查询观看的计划视频数量
     */
    public String countByPlan(Long complanId, Long deptId, Long companyId){
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(new PagerInfo());
        cq.select("count(u.id) num");
        cq.eq("user.companyId", companyId);
        cq.eq("user.deptId", deptId);
        cq.eq("u.complanId", complanId);
        cq.createAlias("user", "user");
        PqGrid pqGrid = pageList(cq);
        EntityMap map = (EntityMap) pqGrid.getRows().get(0);
        String num = map.get("num").toString();
        return num;
    }

    /**
     * 查询用户该课程一共观看的视频数量
     */
    public Integer countByCourse(Long userId, Long courseId){
        return count(q().eq("userId", userId).eq("courseId", courseId));
    }
}
